package com.opener.common.code.assistant.gen;

import org.springframework.util.StringUtils;

import java.util.SortedSet;
import java.util.TreeSet;

public class DaoGenerator extends Generator{
    private static String Suffix = "Dao";

    public DaoGenerator(Ctx ctx, Ctx.MvcClass mvcClass) {
        super(ctx, mvcClass);
    }


    public String createInsertFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        StringBuffer sb = new StringBuffer();
        sb.append("\tInteger add("+modelCls.getClassName()+" "+Utils.lowerFistAlpha(modelCls.getClassName())+");\n");
        return sb.toString();
    }
    public String createUpdateFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        StringBuffer sb = new StringBuffer();
        sb.append("\tboolean update("+modelCls.getClassName()+" "+Utils.lowerFistAlpha(modelCls.getClassName())+");\n");
        return sb.toString();
    }
    public String createGetFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        StringBuffer sb = new StringBuffer();
        sb.append("\t"+modelCls.getClassName()+" get(Integer id);\n");
        return sb.toString();
    }
    public String createSearchFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass searchArgsCls = getCtx().getMvcClass("search");
        StringBuffer sb = new StringBuffer();
        sb.append("\tList<"+modelCls.getClassName()+"> search("+searchArgsCls.getClassName()+" args);\n");
        return sb.toString();
    }

    public String createSearchCountFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass searchArgsCls = getCtx().getMvcClass("search");
        StringBuffer sb = new StringBuffer();
        sb.append("\tInteger getSearchCount("+searchArgsCls.getClassName()+" args);\n");
        return sb.toString();
    }
    public String createImportAnnotation(){
        return "@Repository\n";
    }
    @Override
    public String createJavaString(){
        Ctx.MvcClass cls = getMvcClass();

        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass searchArgsCls = getCtx().getMvcClass("search");
        String pkgLine = "package "+cls.getPkg()+";\n\n";
        SortedSet importLines = new TreeSet();
        importLines.add("import java.util.List;");
        importLines.add("import org.springframework.stereotype.Repository;");
        importLines.add("import "+ modelCls.getPkg()+"."+modelCls.getClassName()+";");
        importLines.add("import "+ modelCls.getPkg()+"."+searchArgsCls.getClassName()+";");
        StringBuffer classBody = new StringBuffer();
        String annotation = createImportAnnotation();
        if (annotation != null)
            classBody.append(annotation);
        classBody.append("public interface "+cls.getClassName()+"{\n");
        String insertFunc = createInsertFunc();
        String updateFunc = createUpdateFunc();
        String getFunc = createGetFunc();
        String searchFunc = createSearchFunc();
        String searchCountFunc = createSearchCountFunc();

        classBody.append(insertFunc);
        classBody.append(updateFunc);
        classBody.append(getFunc);
        classBody.append(searchFunc);
        classBody.append(searchCountFunc);

        classBody.append("}");
        String clsStr = pkgLine +
                StringUtils.collectionToDelimitedString(importLines,"\n") +"\n\n"+
                createClassDescString()+
                classBody.toString();
        return clsStr;
    }
}
